package impl

import (
	"encoding/json"
	"fmt"
	"klotski/pojo"
)

// CertificationObserver 用户认证观察者
type CertificationObserver struct{}

func (o *CertificationObserver) Update(client *pojo.Client, request *pojo.RequestPkg) {
	if request.Type == pojo.CertificationType {
		client.CertificationProcess(*request)
	}
}

// CreateRoomObserver 创建房间观察者
type CreateRoomObserver struct{}

func (o *CreateRoomObserver) Update(client *pojo.Client, request *pojo.RequestPkg) {
	if request.Type == pojo.CreateRoomType {
		client.CreateRoomProcess()
	}
}

// JoinRoomObserver 加入房间观察者
type JoinRoomObserver struct{}

func (o *JoinRoomObserver) Update(client *pojo.Client, request *pojo.RequestPkg) {
	if request.Type == pojo.JoinRoomType {
		var data map[string]interface{}
		if err := json.Unmarshal([]byte(request.Data), &data); err != nil {
			fmt.Println("解析 JSON 失败：", err)
		}
		if uuidValue, ok := data["uuid"].(string); !ok {
			fmt.Println("uuid 字段不存在或不是字符串类型")
			return
		} else {
			client.JoinRoomProcess(uuidValue)
		}
	}
}

// RefreshScoreObserver 刷新游戏分数观察者
type RefreshScoreObserver struct{}

func (o *RefreshScoreObserver) Update(client *pojo.Client, request *pojo.RequestPkg) {
	if request.Type == pojo.RefreshScoreType {
		client.RefreshScoreProcess(*request)
	}
}

// DiscontinueQuitObserver 主动断开连接观察者
type DiscontinueQuitObserver struct{}

func (o *DiscontinueQuitObserver) Update(client *pojo.Client, request *pojo.RequestPkg) {
	if request.Type == pojo.DiscontinueQuitType {
		client.DiscontinueQuitProcess()
	}
}

// GameOverObserver 游戏结束观察者
type GameOverObserver struct{}

func (o *GameOverObserver) Update(client *pojo.Client, request *pojo.RequestPkg) {
	if pojo.GameOverType == request.Type {
		fmt.Println("GameOverType")
	}
}

// HeartCheckObserver 健康检测观察者
type HeartCheckObserver struct{}

func (o *HeartCheckObserver) Update(client *pojo.Client, request *pojo.RequestPkg) {
	if pojo.HeartCheckType == request.Type {
		if request.Data == "PING" {
			client.HeartCheckProcess()
		}
	}
}

/*
switch requestPkg.Type {
		case pojo.CertificationType:
			//用户认证
			client.CertificationProcess(requestPkg)

		case pojo.CreateRoomType:
			//创建房间号,并将创建者加入房间
			client.CreateRoomProcess()

		case pojo.JoinRoomType:
			//1.加入房间的前提,先建立连接
			//2.完成用户认证
			//3.发送消息类型和房间号 Type uuid
			//只有完成上述步骤,才可以加入房间
			var data map[string]interface{}
			err = json.Unmarshal([]byte(requestPkg.Data), &data)
			if err != nil {
				fmt.Println("解析 JSON 失败：", err)
				return
			}
			uuidValue, ok := data["uuid"].(string)
			if !ok {
				fmt.Println("uuid 字段不存在或不是字符串类型")
				return
			}
			client.JoinRoomProcess(uuidValue)

		case pojo.RefreshScoreType:
			//什么是否进行分数更新，前端判断 type:RefreshScoreType, data:step、step、score
			//当用户的行为触发前端游戏机制的更新时，前端调用此接口，后端进行分数的转发 不需要做业务处理，直接转发即可
			fmt.Println("游戏交换中数据", client)
			client.RefreshScoreProcess(requestPkg)

		case pojo.DiscontinueQuitType:
			client.DiscontinueQuitProcess()

		case pojo.GameOverType:
			//游戏结束类型好像没有太大用，游戏结束的时候的提醒，通过分数更新就可以实现了
			fmt.Println("GameOverType")

		case pojo.HeartCheckType:
			//开启一个协程遍历hub中的Client，进行健康检测，生命时间是否会过期，如果过期进行逻辑删除和关闭连接
			if requestPkg.Data == "PING" {
				client.HeartCheckProcess()
			}
		}
*/
